package com.webside.front.grabvideo.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.webside.front.common.model.BSeries;
import com.webside.front.common.model.BVideo;
import com.webside.front.common.model.Bdictionary;
import com.webside.front.common.model.VideoFromWeb;
import com.webside.front.common.service.BAdPositionService;
import com.webside.front.common.service.BSeriesService;
import com.webside.front.common.service.BVideoFromWebService;
import com.webside.front.common.service.BVideoService;
import com.webside.front.common.service.BdictionaryService;
import com.webside.front.detail.service.IDetailService;
import com.webside.front.listener.BaseData;
import com.webside.util.Common;

@Controller
@Scope("prototype")
@RequestMapping(value = "/detail/")
public class GrabVideoController {
	@Autowired
	BVideoService bVideoService;
	@Autowired 
	IDetailService detailService;
	@Autowired
	BSeriesService bSeriesService;
	@Autowired
	BVideoFromWebService bVideoFromWebService;
	@Autowired
	BdictionaryService bdictionaryService;
	@Autowired
	BAdPositionService bAdPositionService;

	@RequestMapping(value = "grabvideo.html", method = { RequestMethod.GET, RequestMethod.POST })
	public String grabVideoView(Model m, Integer id) {
		Integer seriesId = id;
		if (seriesId == null) {
			try {
				throw new Exception("未传入id");
			} catch (Exception e) {
				e.printStackTrace();
				return Common.BACKGROUND_PATH + "/error/error-500";
			}
		} // 取得bSeries的对应对象
		Map<String, Object> bSeriesMap = new HashMap<String, Object>();
		bSeriesMap.put("id", seriesId);
		System.out.println(seriesId);
		List<BSeries> bSeriesList = bSeriesService.selectBseriesByCondition(bSeriesMap);
		System.out.println(bSeriesList);
		if (!(bSeriesList.size() > 0)) {
			return Common.BACKGROUND_PATH + "/error/error-500";
		}
		BSeries bSeries = bSeriesList.get(0);
		// 查来源
		String a[] = bSeries.getSourceList();
		Map<String, Object> sources = getSource(a);
		m.addAttribute("sources", sources);
		String area = bSeriesService.selectAreaNameById(seriesId);
		m.addAttribute("area", area);
		m.addAttribute("bSeries", bSeries);
		m.addAttribute("baseImgPath", BaseData.img_base_path);
		Map<String, Object> condition = new HashMap<String, Object>();
		condition.put("bindId", id);
		// 需求上暂时没有必须来源的理由所以暂时不需要传来源，但接口仍提供本功能
		// condition.put("source", 360);
		condition.put("status", "PUBLISHED");
		condition.put("pageSize", 10);
		List<VideoFromWeb> webVideo = bVideoFromWebService.selectWebVideoByCondition(condition);
		m.addAttribute("webVideo", webVideo);
		//总集数
		m.addAttribute("videoCount", this.getPagesCount(seriesId));
		// 评论数
		int c_Count = detailService.CommentCount(bSeries.getId());
		m.addAttribute("c_Count", c_Count);
		// 看过此片还喜欢
		BSeries conditionSeries = new BSeries();

		conditionSeries.setType(bSeries.getType());
		/* conditionSeries.setType("MOVIE_ACTION"); */
		conditionSeries.setId(227);
		List<BSeries> mayLikeSeries = detailService.selectMayLike(conditionSeries);
		m.addAttribute("mayLikeSeriesList", mayLikeSeries);
		// 排行推荐
		conditionSeries.setChannel(bSeries.getChannel());
		/* conditionSeries.setChannel("MOVIE"); */
		List<BSeries> mayChannelSeries = detailService.selectMayChannel(conditionSeries);
		m.addAttribute("mayChannelSeriesList", mayChannelSeries);
		
		
		/***********************************广告部分测试，后续需要修改*******************************************/
		/*                                                                                               */
		/*                                                                                               */
		//通栏
		m.addAttribute("bannerOne", bAdPositionService.selectPositionByCode("BANNERONE"));
		//矩形
		m.addAttribute("rectThree", bAdPositionService.selectPositionByCode("RECTTHREE"));
		/*                                                                                               */
		/*                                                                                               */
		/*                                                                                               */
		/*************************************************************************************************/
		/*
		 * 搜索关键字 
		 */
		m.addAttribute("keyWords",this.getSearchKeyWords());
		
		if(bSeries.getChannel().equals("VARITY")){
			Map<String,Object> map = new HashMap<String, Object>();
			map.put("seriesId", seriesId);
			map.put("orderColumn", "set_num");
			map.put("orderType", "DESC");
			map.put("pageSize", 4);
			List<BVideo> list = bVideoService.selectBVideoByCondition(map);
			m.addAttribute("list", list);
			return Common.BACKGROUND_PATH + "/front/detailpage/varityDetail";
		}
		
		return Common.BACKGROUND_PATH + "/front/detailpage/detail";
	}
	
	@RequestMapping(value = "getvideo", method = { RequestMethod.POST })
	public @ResponseBody List<BVideo> getVideo(Integer seriesId) {
		Map<String, Object> condition = new HashMap<String, Object>();
		condition.put("seriesId", seriesId);
		condition.put("orderColumn", "set_num");
		condition.put("orderType", "ASC");
		List<BVideo> list = bVideoService.selectBVideoByCondition(condition);
		return list;
	}
	
	@RequestMapping(value = "getvideos", method = { RequestMethod.POST })
	public @ResponseBody List<BVideo> getVideo(Integer seriesId,String pageNo) {
		Map<String, Object> condition = new HashMap<String, Object>();
		condition.put("seriesId", seriesId);
		condition.put("orderColumn", "set_num");
		condition.put("orderType", "DESC");
		condition.put("pageSize", 10);
		System.out.println(pageNo);
		Integer pgNo = (Integer.parseInt(pageNo)-1)*10;
		condition.put("pageNo", pgNo);
		List<BVideo> list = bVideoService.selectBVideoByCondition(condition);
		System.out.println(list);
		return list;
	}
	public Map<String, Object> getSource(String[] sources) {
		Map<String, Object> sourceMap = new HashMap<String, Object>();
		if (sources == null) {
			return null;
		}
		for (String string : sources) {
			String name = string.substring(0, 2);
			System.out.println(name);
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("name", name);
			map.put("type", "SOURCE");
			List<Bdictionary> list = bdictionaryService.selectBdictionaryByCondition(map);
			System.out.println(list.size());
			if (list.size() > 0) {
				Bdictionary b = list.get(0);
				System.out.println(b.getCode());
				sourceMap.put(b.getCode(), b);
			}

		}
		return sourceMap;
	}
	
	/**
	 * 获取集数的页数
	 */
	private int getPagesCount(Integer id){
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("seriesId", id);
		//map.put("status", "PUBLISHED");//(暂时没用到)
		int videosCount=bVideoService.selectVideosCountByCondition(map);
		int pageCount =  (int)Math.ceil((double)videosCount/10);
		return pageCount;
	}
	
	/**
	 * 获取搜寻关键字
	 */
	private List<String> getSearchKeyWords(){
		Map<String, Object> keyWords = new HashMap<String, Object>();
		keyWords.put("type", "SEARCH_KEYWORDS");
		keyWords.put("code","SEARCH_KEYWORDS");
		List<Bdictionary> list = bdictionaryService.selectBdictionaryByCondition(keyWords);
		List<String> keyWord = new ArrayList<String>();
		String[] str = new String[]{};
		for (Bdictionary b : list) {
			str = b.getName().split(",");
			List<String> l = Arrays.asList(str);
			keyWord.addAll(l);
		}
		return keyWord;
	}

}
